/*
PAGE DOCUMENTATION:
==================
This JavaScript file contains JS functions that are used  more then others.
They have been separated to allow FB caching of this file.
*/


function highlightRow(obj) 
{
	obj.setStyle('background','#FFF');
}

function cancelRow(obj)
{
	if (obj.getClassName()=="notRead") {
		obj.setStyle('background','#c3cddf');
	} else {
		obj.setStyle('background','#f7f7f7');
	}
}

function showDialog(url,title, ok, cancel) {   
    // Set the default pop-up dialog values for those not supplied.   
    if (title === undefined) { title = "Dialog"; }   
    if (ok === undefined) { ok = "Okay"; }   
    if (cancel === undefined) { cancel = "Cancel"; }   
  
    // Build the AJAX object to request the dialog contents.   
    var ajax = new Ajax();   
    ajax.responseType = Ajax.FBML;   
    ajax.requireLogin = true;   
    ajax.ondone = function(data) {   
        // Fill the Dialog's DIV element with the FBML returned by the AJAX request.   
        // This will overwrite the loading message FBML currently nested in the DIV.   
        document.getElementById('dialog_content').setInnerFBML(data);  
    };  
 
    // Create a "loading" dialog box that will be updated via the AJAX request.  
    dialog = new Dialog().showChoice(title, dialog_fbml, ok, cancel);  
    dialog.onconfirm = function() {
		// validate user input. if ok, open record dialog. if not, report error.
		var name = document.getElementById("textBox").getValue();
		if (validateName(name)) {
			var newFileName = document.getElementById("fileBox").getValue();
			var recipient = document.getElementById("recipientBox").getValue();
			dialog.hide();
			performRecord(name,newFileName,recipient);
		} else {
			dialogError = new Dialog().showMessage("No Name typed!", "The message name field is a mandatory one",'Okay');
			dialogError.onconfirm = function() {
				dialogError.hide();
				// create naming dialog again (at "onconfirm" DOM element is gone)
				showDialog(url,title, ok, cancel);
			};
		}  
    };   
    // Update the dialog contents via Mock AJAX request.   
    ajax.post(url);   
}  

function performRecord(name,file,recipient) {
	var recordDialog = new Dialog().showMessage("Recording", dialog_fbml,"Cancel");
//	var nameForURL = encodeURIComponent(name);
	var nameForURL = encodeStr(name);
	var ajaxRecord = new Ajax();   
	ajaxRecord.responseType = Ajax.FBML;   
	ajaxRecord.requireLogin = true;   
	ajaxRecord.ondone = function(data) {   
		document.getElementById('dialog_content').setInnerFBML(data);  
	};
//	var queryParams = {message_name: nameForURL,newFileName: file,recipient: recipient};
//	ajaxRecord.post('server + "/flashFBML.php', queryParams);
	ajaxRecord.post(server + "/flashFBML.php?message_name=" + escape(nameForURL) + "&newFileName=" + file + "&recipient=" + recipient);   
	recordDialog.onconfirm = function() {
		recordDialog.hide(); 
	};
}

function validateName(input) {
	if (input!='Click to type' && input!='') {
		return 1;
	} else {
		return 0;
	}
}

function encodeStr(str) {
	return str.replace(/ /g, "%20"); 
}

function trim(str) {   
    return str.replace(/^\s+|\s+$/g, '');   
}   
  
function clearInput(e, compare) {   
    e.removeClassName('inputblank');   // remove grayed out color   
    if (e.getValue() == compare) {   
        e.setValue('');                // clear default text when clicked   
    }   
}   
  
function restoreInput(e, value) {   
    if (trim(e.getValue()) == '') {   
        e.setValue(value);             // reset default text when unclicked   
        e.addClassName('inputblank');  // display grayed out color   
    }   
}   

function showPlayDialog(filename, messageName, index, isOld, sourceURL) {
	Animation(document.getElementById("options_" + index).getParentNode().getParentNode().getParentNode()).hide().go();
	//create play dialog:
	playDialog = new Dialog().showMessage("Playing \"" + messageName + "\"", dialog_fbml,"Done");
	var ajaxRecord = new Ajax();   
	ajaxRecord.responseType = Ajax.FBML;   
	ajaxRecord.requireLogin = true;   
	ajaxRecord.ondone = function(data) {   
		document.getElementById('dialog_content').setInnerFBML(data);  
	};
	if (!isOld) {
		ajaxRecord.post(server + "/playDialog.php?filename=" + filename + "&index=" + index + "&is_read=" + isOld + "&sourceURL=" + sourceURL);  
	} else {
		ajaxRecord.post(server + "/playDialog.php?filename=" + filename);  
	}
	playDialog.onconfirm = function() {
		if (!isOld) {
			dialog = new Dialog().showMessage("How you doing??", dialog_fbml, "ok"); 
			frm = document.getElementById("play_" + index);
			if (frm) { 
				frm.submit(); 
			}
		}
	}
}
